package com.csaba.connector.khb.model;

import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;

import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.csaba.connector.ServiceException;
import com.csaba.connector.khb.Codes;
import com.csaba.connector.web.util.WebSession;
import com.csaba.util.LoggerHelper;

/**
 * @author user
 *
 */
public class KHBSession extends WebSession implements Codes
{
	private static final long serialVersionUID = -9076501680857723605L;

	public KHBSession()
	{
		setBank(KHBBank.getInstance());

		//set host config because K&H using NetLock certs that is not trusted by the JRE by default.
		/* NetLock cert is not used anymore. No hack is required. Thanks KHB! :)
		 * final SSLSocketFactory sf = new SSLSocketFactory(NetLockCertificate.installCert());
		final Scheme scheme = new Scheme(URL_EBANK_SCHEME, sf, URL_EBANK_PORT);
		final SchemeRegistry registry = new SchemeRegistry();
		registry.register(scheme);

		final HttpParams params = new BasicHttpParams();

		final ClientConnectionManager cm = new ThreadSafeClientConnManager(params, registry);

		setConnectionManager(cm);*/

		final HttpHost host = new HttpHost(URL_EBANK_HOST, URL_EBANK_PORT, URL_EBANK_SCHEME);

		setHost(host);

		setDummyInterval(0); //TODO disable dummy for the time being; reconsider later on, when dummyservice is implemented.
	}

	public static void main( final String[] args ) throws ServiceException, NoSuchAlgorithmException
	{
		LoggerHelper.initLogger(Level.FINEST);

		final KHBSession khb = new KHBSession();
		khb.setDebugFilePath("../khb.source/test/");

		//load login page for session ID
		final NameValuePair[] indexParams = new NameValuePair[] { //
		new BasicNameValuePair("fun", FUN_LOGIN_PREPIN),//
				new BasicNameValuePair("lang_id", "hu"),//
		};

		final String indexResult = khb.doPost(Codes.URL_RETAIL_ROOT, indexParams);
		khb.debugFile("index", indexResult);

		System.out.println(" Session ID: " + khb.getSessionId());

		//post PIN and get SMS code
		final NameValuePair[] loginParams = new NameValuePair[] { //
		new BasicNameValuePair("fun", FUN_LOGIN_PRESMS),//
				new BasicNameValuePair("lite", "1"),//
				new BasicNameValuePair("lang_id", "hu"),//
				new BasicNameValuePair("pan_prefix", LOGIN_PREFIX),//
				new BasicNameValuePair("lite_pan", args[0]),//
				new BasicNameValuePair("lite_pin", args[1]),//
				new BasicNameValuePair("submit", "belépés"),//
		};
		//url: /Ebank2%3Ffun%3D0.0.L.L%26dummy%3D1224935913451
		final String loginResult = khb.doPost(Codes.URL_RETAIL_ROOT, loginParams);
		khb.debugFile("login", loginResult);

		//             * invalid user id: <td align="left" valign="top" class="confirm_data">Sikertelen bejelentkezĂŠsi kisĂŠrlet.</td>

		//post SMS code
		final NameValuePair[] smsParams = new NameValuePair[] { //
		new BasicNameValuePair("fun", FUN_LOGIN_SMS),//
				new BasicNameValuePair("sign", "0b5b5ee3cbdb91626a04d2003e18f7864f04f61a"),//where does it come?
				new BasicNameValuePair("sign_this", "5BE4366252D3"),//comes from hiden field resulted in PRESMS request
				new BasicNameValuePair("lang_id", "hu"),//
				new BasicNameValuePair("pg_id", "47FDE732"),//comes from hiden field resulted in PRESMS request
				new BasicNameValuePair("nonstandard", "00ED00E1"),//where does it come?
				new BasicNameValuePair("logintype", "e-bank SMS azonosítással"),//comes from hiden field resulted in PRESMS request
				new BasicNameValuePair("lite_password", ""),//this is the password field, but the content is encoded by javascript, so the value comes in the sign or nonstandard fields.
				new BasicNameValuePair("submit1", "belépés"),//ID of the button to press
		};

		/* Algorythm to get _sign_ variable:
		 * - concatinate name-value pairs as follows: sign_this=xxx|logintype=yyy|lite_password=zzz
		 * - escape it: use escapeIt() method
		 * - generate _nonstandard_ using the tellNonStandard() with the same seed
		 * - generate _sign_ using hex_sha1() method: Util.hexSha1()
		 */
		//url: /Ebank2%3Ffun%3D0.0.L.L%26dummy%3D1224935913451
		final String smsResult = khb.doPost(Codes.URL_RETAIL_ROOT, smsParams);
		khb.debugFile("loginsms", smsResult);

		//logout (dummy and pg_id is missing currently - hope, this is not necessary)
		final String logoutResult = khb.doGet(Codes.URL_RETAIL_ROOT + "?fun=" + FUN_LOGOUT);
		khb.debugFile("logout", logoutResult);
		final String logout2Result = khb.doGet(Codes.URL_RETAIL_ROOT + "?fun=" + FUN_LOGIN_INDEX);
		khb.debugFile("logout2", logout2Result);
		//close session
		khb.invalidateSession();
	}
}
